package com.financing.platform.task;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.financing.platform.dao.IForegroundProductDao;
import com.financing.platform.dao.IMessageSmsDao;
import com.financing.platform.dto.MessageSmsDTO;
import com.financing.platform.util.Dict;
import com.financing.platform.util.MessageUtil;

/**
 * 
* @ClassName: SmsSendThread 
* @Description: 
* @author chenming 
* @date 2014年9月22日 下午6:32:23 
*
 */
@Component(value="smsSendOutProductUPdate" )
public class SmsSendOutProductUpdateJob {
	
	@Autowired
	private IForegroundProductDao foregroundProductDao;
	
	@Autowired
	private IMessageSmsDao messageSmsDao;
	
	Logger log = Logger.getLogger(this.getClass());
	//去除 更新募集进度邮件
//	@Scheduled(cron = "0 0 10 * * ?") 
	public void sendMessageJob(){
		//时间判断
		Calendar cal=Calendar.getInstance();
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if(w==6||w==7){
			log.info("week....  not send");
			return ;
		}
		
		MessageSmsDTO messageSmsDTO  = new MessageSmsDTO();
		log.info("addSendMessageJob begin run");
		messageSmsDTO.setFpSmsSendTime(null);
		messageSmsDTO.setFpSmsSendType(Dict.message_type_now);
		messageSmsDTO.setFpSmsSendStatus(Dict.message_status_not_send);
		messageSmsDTO.setFpSmsSendTimes(Dict.message_max_send_times);
		messageSmsDTO.setUpdateBy("system");
		messageSmsDTO.setUpdateDate(new Date());
		messageSmsDTO.setCreateBy("system");
		messageSmsDTO.setCreateDate(new Date());
		messageSmsDTO.setFpSmsType(Dict.sms_send_switch);
		messageSmsDTO.setStatus(Dict.message_status_not_send);
		try {
			List<Map<String, Object>> queryForegroundProductBySendSMS = foregroundProductDao.queryForegroundProductBySendSMS();
			for (int i = 0; i < queryForegroundProductBySendSMS.size(); i++) {
				try {
					Map<String, Object> map = queryForegroundProductBySendSMS.get(i);
					Map<String, Object> message = MessageUtil.getMessage("product", "outProductUpdate", map);
					String content = (String) message.get("content");
					String mobile=(String)map.get("mobile");
					messageSmsDTO.setFpSmsRecivePhoneNo(mobile);
					messageSmsDTO.setFpSmsContent(content);
					messageSmsDao.addMessageSms(messageSmsDTO);
					messageSmsDTO.setFpSmsRecivePhoneNo("");
					messageSmsDTO.setFpSmsContent("");
				} catch (Exception e) {
					log.error("queryForegroundProductBySendSMS add error :"+messageSmsDTO.getFpSmsRecivePhoneNo(),e);
				}
			}
		} catch (SQLException e) {
			log.error("queryForegroundProductBySendSMS error :",e);
		}
	}
}